package com.jrx.anytxn.customer.controller;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.customer.dto.BaseApiReq;
import com.jrx.anytxn.customer.dto.BaseApiRes;
import com.jrx.anytxn.customer.dto.bankcardinfo.*;
import com.jrx.anytxn.customer.handel.IBankCardInfoHandler;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 银行卡绑定
 *
 * @author Yang Xin
 * @date 2019-07-18 15:21
 **/
@Api(tags = "银行卡管理的api接口")
@RestController
@RequestMapping(value = "/api/customer/bankcard")
public class BankCardController {
    private static Logger logger = LoggerFactory.getLogger(BankCardController.class);
    @Autowired
    private IBankCardInfoHandler bankCardInfoHandler;

    @ApiOperation(value = "银行卡绑定API", notes = "在消费信贷核心系统中建立客户的银行卡信息,银行卡绑定API")
    @PostMapping("/bind")
    public TxnRespResult<BankCardBindRes> bindBankCard(@Valid @RequestBody BankCardBindReq bankCardBindReq) throws Exception {
        TxnRespResult<BankCardBindRes> result = new TxnRespResult<>();
        try {
            BankCardBindRes res=bankCardInfoHandler.registerBankCardBind(bankCardBindReq);
            result.getSuccess(res);
        } catch (TxnBizException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation(value = "银行卡解绑API", notes = "在消费信贷核心系统中为客户解绑银行卡,银行卡解绑API")
    @PostMapping("/unbind")
    public TxnRespResult<BaseApiRes> unBindBankCard(@Valid @RequestBody BankCardUnBindReq bankCardUnBindReq) {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        try {
            BaseApiRes res=bankCardInfoHandler.modifyBankCardUnBind(bankCardUnBindReq);
            result.getSuccess(res);
        } catch (TxnBizException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation(value = "银行卡列表查询API", notes = "银行卡列表查询API")
    @PostMapping("/getBankCardList")
    public TxnRespResult<BankCardListRes> getBankCardList(@Valid @RequestBody BaseApiReq bankCardListReq) {
        TxnRespResult<BankCardListRes> result = new TxnRespResult<>();
        try {
            BankCardListRes res=bankCardInfoHandler.getBankCardList(bankCardListReq);
            result.getSuccess(res);
        } catch (TxnBizException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation(value = "银行卡设置默认收还款标志", notes = "银行卡设置默认收还款标志")
    @PostMapping("/modifyFlagBankCard")
    public TxnRespResult<BaseApiRes> modifyFlagBankCard(@Valid @RequestBody BankCardSetFlagReq bankCardSetFlagReq) throws Exception{
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        try {
            BaseApiRes res=bankCardInfoHandler.modifyFlagBankCard(bankCardSetFlagReq);
            result.getSuccess(res);
        } catch (TxnBizException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }
}
